<template>
	<view class="container" :style="{ height: windowHeight + 'px' }">
		<view class="content">
			<view class="login-account-title">欢迎登录穗穗</view>
			<input :focus="true" :adjust-position="false" class="login-account-input" placeholder-class="login-account-placeholder" type="number" v-model="mobile" placeholder="请输入手机号" />
			<input v-if="loginMethod == 'password'" :adjust-position="false" class="login-account-input" placeholder-class="login-account-placeholder" type="text" v-model="password" placeholder="请输入密码" password="password" />
			<view class="login-account-tips" v-if="loginMethod == 'code'">未注册的手机号验证后自动创建穗穗账号</view>
			<view class="login-account-tips" v-if="loginMethod == 'password'">未设置密码或未注册的账号请使用短信验证码登录</view>
			<button v-if="loginMethod == 'code'" @tap.stop="getCodeToPage" class="action-btn" :class="{ able: mobile }">获取短信验证码</button>
			<button v-if="loginMethod == 'password'" @tap.stop="loginInPassword" class="action-btn" :class="{ able: mobile && password }">账号密码登录</button>
			<view class="alternative">
				<view class="method" @tap.stop="loginMethod = 'password'" v-if="loginMethod == 'code'">密码登录</view>
				<view class="method" @tap.stop="loginMethod = 'code'" v-if="loginMethod == 'password'">验证码登录</view>
				<view class="issue">遇到问题</view>
			</view>
		</view>
		<view class="buttom">
			<template v-if="!showAuthLoginType">
				<view class="login-type-tips">
					<view class="login-type-tips-line"></view>
					<view class="login-type-tips-text">使用快捷登录</view>
					<view class="login-type-tips-line"></view>
				</view>
				<view class="login-type">
					<view class="login-type-item" v-if="loginType.wechatApp"><image class="login-type-item-image" src="@/static/images/auth/wechat.png" mode="aspectFit"></image></view>
					<view class="login-type-item" v-if="loginType.apple"><image class="login-type-item-image" src="@/static/images/auth/apple.png" mode="aspectFit"></image></view>
				</view>
			</template>
			<view class="hint">
				登录即代表同意
				<text class="login-account-link"  @tap.stop="toAgreePage(0)" :data-index="0">用户协议</text>
				和
				<text class="login-account-link"  @tap.stop="toAgreePage(4)" :data-index="4">隐私政策</text>
				并授权使用您的穗穗信息（如昵称、头像、性格信息）以便您统一管理
			</view>
		</view>
	</view>
</template>

<script>
import { updateUserToken } from '@/store/dispatches.js';
import Encryption from '@/utils/Encryption.js';
import { initIMService } from '@/store/dispatches.js';
import AgreemenType from '@/utils/enum/AgreemenType.js';
export default {
	data() {
		return {
			mobile: '',
			password: '',
			windowHeight: null,
			loginType: {
				wechatApp: false,
				apple: false
			},
			identity: 10,
			placeholderStyle: 'font-size: 32rpx;font-weight: 400;color:grey;',
			loginMethod: 'code' ,//password
			agreemenType:AgreemenType
		};
	},
	computed: {
		showAuthLoginType: function() {
			return !this.loginType.apple && !this.loginType.wechatApp;
		}
	},
	onLoad(options) {
		this.identity = options.identity || 10;
		this.$nextTick(() => {
			const systemInfo = uni.getSystemInfoSync();
			this.windowHeight = systemInfo.windowHeight;
		});
	},
	onShow() {},
	methods: {
		toAgreePage(index){
			this.navigateTo({
				url: this.$u.api.openAgreementPage(this.agreemenType[index].value)
			});
		},
		getCodeToPage() {
			this.navigateTo({
				url: '/pageUser/login/code?mobile=' + this.mobile + '&identity=' + this.identity
			});
		},
		loginInPassword() {
			this.$u.api.loginByPassword(this.mobile, Encryption.aesToPassword(this.password)).then(res => {
				updateUserToken(res.data.token);
				initIMService();
				this.refreshUserInfo();
			});
		},
		completeUserInfo() {
			this.reLaunch();
		}
	}
};
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	justify-content: center;
	align-items: flex-start;
	flex-wrap: wrap;
	font-size: 28rpx;
	overflow: hidden;
	background-color: #fff;
	.content {
		width: 650rpx;
		margin: 30rpx auto 0;

		.login-account-title {
			text-align: left;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}
		.login-account-input {
			text-align: left;
			margin-bottom: 10rpx;
			padding-bottom: 6rpx;
			height: 80rpx;
			font-size: 46rpx;
			letter-spacing: 2rpx;
			font-weight: 600;
		}
		.login-account-placeholder {
			font-size: 32rpx;
			font-weight: 400;
		}
		.login-account-tips {
			color: $u-type-info;
			margin-bottom: 60rpx;
			margin-top: 8rpx;
			font-size: 23rpx;
		}
		.action-btn {
			background-color: rgb(254, 223, 208);
			color: $u-tips-color;
			border: none;
			font-size: 30rpx;
			padding: 12rpx 0;
			&.able {
				background-color: $theme-main-color;
				color: #fff;
			}

			&::after {
				border: none;
			}
		}
		.alternative {
			color: $u-tips-color;
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
		}
	}
	.buttom {
		align-self: flex-end;
		.login-type-tips {
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 20rpx 0;
			margin-bottom: 20rpx;
			.login-type-tips-line {
				height: 1rpx;
				width: 100rpx;
				background-color: $u-tips-color;
				margin: 0 20rpx;
			}
			.login-type-tips-text {
				font-size: 25rpx;
				color: $u-tips-color;
			}
		}
		.login-type {
			display: flex;
			padding: 0 150rpx 50rpx 150rpx;
			justify-content: center;

			.login-type-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				color: $u-content-color;
				font-size: 28rpx;
				padding: 0 30rpx;
				.login-type-item-image {
					width: 80rpx;
					height: 80rpx;
				}
			}
		}

		.hint {
			padding: 20rpx 40rpx 40rpx;
			font-size: 20rpx;
			line-height: 30rpx;
			color: $u-tips-color;
			text-align: center;

			.login-account-link {
				color: $theme-main-color;
				padding: 0 4rpx;
				text-decoration: underline;
			}
		}
	}
}
</style>
